using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum ScrollType
{
    UP,
    DOWN,
    LEFT,
    RIGHT
}

public class ScrollView : MonoBehaviour
{
    public GameObject scrollItemGo;
    private RectTransform content;
    
    [SerializeField]
    private float spacing;
    private bool isStart = true;
    
    void Start()
    {
        content = this.GetComponent<ScrollRect>().content;
        spacing = 15;
        OnAddHead();
    }
    
    private GameObject GetChildItem()
    {
        //查找是否有未回收的子节点
        for (int i = 0; i < content.childCount; ++i)
        {
            GameObject tempGo = content.GetChild(i).gameObject;
            if (!tempGo.activeSelf)
            {
                tempGo.SetActive(true);
                return tempGo;
            }
        }

        //无创建新的
        GameObject childItem = GameObject.Instantiate<GameObject>(scrollItemGo,content.transform);
        ScrollViewItem scrollItem = childItem.GetComponent<ScrollViewItem>();
        if (scrollItem == null)
            scrollItem = childItem.AddComponent<ScrollViewItem>();
        
        scrollItem.onAddHead += OnAddHead;
        scrollItem.onRemoveHead += OnRemoveHead;
        scrollItem.onAddEnd += OnAddEnd;
        scrollItem.onRemoveEnd += OnRemoveEnd;
        scrollItem.Init();

        childItem.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 1);
        childItem.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 1);
        childItem.GetComponent<RectTransform>().pivot = new Vector2(0, 1);
        
        childItem.transform.localScale = Vector3.one;
        childItem.transform.localPosition = Vector3.zero;
        
        //-----设置宽高——加载数据 
        return childItem;
    }

    private void OnAddHead()
    {
        Data data = this.GetComponent<Test>().scrollData.GetHeadData();

        if (data != null)
        {
            Transform first = FindFirst();
            //----first 不为 数据头---在data中做了
            GameObject obj = GetChildItem();
            obj.GetComponent<ScrollViewItem>().RefreshData(data);
            obj.transform.SetAsFirstSibling();

            RectTransform objRect = obj.GetComponent<RectTransform>();
            float height = objRect.sizeDelta.y;
            
            if (first != null)
            {
                obj.transform.localPosition = first.localPosition + new Vector3(0, height + spacing, 0);
            }
            
            if (isStart)
            {
                content.sizeDelta += new Vector2(0, height + spacing);
                isStart = false;
            }
        }
    }

    private void OnRemoveHead()
    {
        var scrollData = this.GetComponent<Test>().scrollData;
        if (scrollData.RemoveHeadData())
        {
            Transform tf = FindFirst();
            if (tf != null)
                tf.gameObject.SetActive(false);
        }
    }

    private void OnAddEnd()
    {
        Data data = this.GetComponent<Test>().scrollData.GetEndData();

        if (data != null)
        {
            Transform end = FindEnd();
            //----end 不为 数据尾在data中做了
            GameObject obj = GetChildItem();

            obj.transform.SetAsLastSibling();

            obj.GetComponent<ScrollViewItem>().RefreshData(data);

            float height = end.GetComponent<RectTransform>().sizeDelta.y;

            if (end != null)
                obj.transform.localPosition = end.localPosition - new Vector3(0, height + spacing, 0);

            //是否增加content高度
            if (IsAddContentH(obj.transform))
            {
                float h = obj.GetComponent<RectTransform>().sizeDelta.y;
                content.sizeDelta += new Vector2(0, h + spacing);
            }
        }
    }

    private void OnRemoveEnd()
    {
        var scrollData = this.GetComponent<Test>().scrollData;
        if (scrollData.RemoveEndData())
        {
            Transform tf = FindEnd();
            if (tf != null)
                tf.gameObject.SetActive(false);
        }
    }

    private Transform FindFirst()
    {
        for (int i = 0; i < content.childCount; ++i)
        {
            if (content.GetChild(i).gameObject.activeSelf)
            {
                return content.GetChild(i);
            }
        }
        return null;
    }

    private Transform FindEnd()
    {
        for (int i = content.childCount - 1; i >= 0; --i)
        {
            if (content.GetChild(i).gameObject.activeSelf)
            {
                return content.GetChild(i);
            }
        }
        return null;
    }
    
    private bool IsAddContentH(Transform tf)
    {
        Vector3[] rectC = new Vector3[4];
        Vector3[] contentC = new Vector3[4];
        tf.GetComponent<RectTransform>().GetWorldCorners(rectC);
        content.GetWorldCorners(contentC);
        if (rectC[0].y < contentC[0].y)
            return true;
        return false;
    }
}


